package com.example.base.utils;

import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileSha56Checksum {

    public static String getSha56Checksum(String path) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        try (DigestInputStream dis = new DigestInputStream(new FileInputStream(path), md)) {
            byte[] buffer = new byte[4 * 1024];
            while (dis.read(buffer) != -1) {
                // 读取文件内容以更新摘要
            }
        }
        byte[] hash = md.digest();
        // 将摘要转换为十六进制字符串
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        String shaValue = hexString.toString();
        System.out.println("SHA-256值: " + shaValue);
        return shaValue;
    }

    public static void main(String[] args) throws Exception {
        String filePath = "D:\\video\\BEYOND - 海阔天空.mp4";
        String v1=FileSha56Checksum.getSha56Checksum(filePath);
        String v2=FileMD5Checksum.getMD5Checksum(filePath);
        System.out.println(v1+" "+v2);
    }
}
